package com.netbetter.langzheng.plugin.fileManager;

import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binder.TreeBinder;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.StoreSorter;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.tree.Tree;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.netbetter.langzheng.commons.constants.AppEvents;
import com.netbetter.langzheng.commons.constants.VersionConstants;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeTreeItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.data.FileManagerRequest;
import com.netbetter.langzheng.plugin.fileManager.dataProxy.FileModelDataProxy;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;
import com.netbetter.langzheng.plugin.fileManager.reader.FolderInfoReader;
import com.netbetter.langzheng.plugin.fileManager.widget.tree.LangzhengTree;

/**
 * 目录导航树
 * 
 * @author 嘉陵江里捉鱼
 */
public class FolderNavigationView extends View {

    private Tree                  tree;
    private TreeStore<FileModel>  store;
    private VersionConstants      versionConstant;
    private TreeBinder<FileModel> binder;
    private TreeLoader<ModelData> loader;

    public FolderNavigationView(Controller controller) {
        super(controller);
    }

    protected void initialize() {

    }

    protected void initTree() {

        FileModelDataProxy proxy = new FileModelDataProxy(FileManagerRequest.getFolderInfo(null)) {

            /**
             * 通过代理，在不同节点可动态发起请求
             */
            @Override
            public void load(DataReader<FileModel, List<FileModel>> reader, FileModel loadConfig,
                             AsyncCallback<List<FileModel>> callback) {
                if (loadConfig != null) {
                    this.setBuilder(FileManagerRequest.getFolderInfo(loadConfig));
                }
                super.load(reader, loadConfig, callback);
            }

        };
        loader = new BaseTreeLoader(proxy, new FolderInfoReader<ModelData>()) {

            /**
             * 判断是否具备子节点
             */
            @Override
            public boolean hasChildren(ModelData file) {
                if (file != null) {
                    if (file instanceof FileModel) {
                        return ((FileModel) file).getIsDirectory();
                    }
                }
                return false;
            }
        };
        ChangeTreeItemEventContext.setLoader(loader);
        store = new TreeStore<FileModel>(loader);
        store.setStoreSorter(new StoreSorter<FileModel>());
        binder = new TreeBinder<FileModel>(tree, store);
        binder.setAutoSelect(true);
        binder.setIconProvider(new ModelStringProvider<FileModel>() {

            /**
             * 给出图标
             * 
             * @param model
             * @param property
             * @return
             */
            public String getStringValue(FileModel model, String property) {
                return null;
            }
        });
        binder.addSelectionChangedListener(new SelectionChangedListener<ModelData>() {

            /**
             * 选中节点后触发右边list更新数据
             */
            public void selectionChanged(SelectionChangedEvent se) {
                FileModel fileModel = null;
                if (se.getSelection().size() > 0) {
                    fileModel = (FileModel) se.getSelection().get(0);
                    ChangeTreeItemEventContext.attach(fileModel, tree.getSelectedItem());
                    if (fileModel.getId() == -1) {// 跟节点默认将所有节点引导出来
                        loader.loadChildren(fileModel);

                    }
                }
            }
        });
        binder.setDisplayProperty("name");
        loader.load();
    }

    /**
     * 获得当前版本号
     * 
     * @return
     */
    private String getBuilderInfo() {

        versionConstant = GWT.create(VersionConstants.class);
        String versionStr = versionConstant.builderVersion() + "";
        String retVal = versionStr;
        for (int i = versionStr.length(); i < 5; i++) {
            retVal = "0" + retVal;
        }
        retVal = "V:" + retVal;
        return retVal;
    }

    /**
     * 处理listview选中folder
     * 
     * @param fileModel
     */
    protected void selectFolder(FileModel fileModel) {
        // 将右边list请求下来
        AppEvent<?> evt = new AppEvent(AppEvents.ViewLIST, fileModel);
        fireEvent(evt);
    }

    protected void initUI() {
        tree = new LangzhengTree();
        tree.getStyle().setLeafIconStyle("tree-folder");

        ContentPanel panel = new ContentPanel();
        panel.setHeading("数据快递(" + getBuilderInfo() + ")");
        initTree();
        panel.add(tree);
        panel.setScrollMode(Scroll.AUTO);

        LayoutContainer west = (LayoutContainer) Registry.get("west");
        west.setLayout(new FitLayout());
        west.add(panel);
        // 初始化完毕后引导出listview
        Dispatcher.get().dispatch(AppEvents.NAVLASTVIEW);
    }

    protected void handleEvent(AppEvent<?> event) {
        switch (event.type) {
            case AppEvents.Init:
                initUI();
                break;
            case AppEvents.NavFolder:
                selectFolder((FileModel) event.data);
                break;
        }

        if (event.type == AppEvents.NAVLASTVIEW) {

        }
    }
}
